//
//  HYFEmoticonKeyboardView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/29.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

class HYFEmoticonKeyboardView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 216))
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加控件
        addSubview(emotionToolBar)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        
        pageControl.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView).offset(5)
        }
        
        
        emoticonPageView.snp_makeConstraints { (make) in
            
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emotionToolBar.snp_top)
            
        }
        
        
        emotionToolBar.snp_makeConstraints { (make) in
            
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        emotionToolBar.closure = {(type:HYFEmoticonToolBarType)->() in
            
            switch type{
                
            case .Default:
                print("默认")
            case .Emoji:
                print("Emoji")
            case .Lxh:
                print("浪小花")
            }
            
            //获得 IndexPath 
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            //滚动 pageView
            self.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
            
            //设置 PageControl
            
            
            
        }
        
        let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        
        setupPageControlData(indexPath)
        
    }
    
    //懒加载控件
    private lazy var emotionToolBar:HYFEmoticonToolBar = HYFEmoticonToolBar(frame: CGRectZero)

    private lazy var emoticonPageView:HYFEmoticonPageView = {
        
       let view = HYFEmoticonPageView()
        
        view.backgroundColor = self.backgroundColor
        
        view.pagingEnabled = true
        view.bounces = false
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        view.delegate = self
        
        return view
        
    }()
    
    //懒加载 pageControl
    private lazy var pageControl:UIPageControl = {
        
        let view = UIPageControl()
        //设置总页数
        // 在xcode 6.4 可以看到 以后苹果直接就给他隐藏了 但是你知道名字 可以通过kvc赋值
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return view
    }()
    
}

extension HYFEmoticonKeyboardView:UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //获取相对于 collectionView 中心点 X
        let centerX = scrollView.contentOffset.x + SCREENW * 0.5
        
        //拿到屏幕显示 的 cells 数组.并进行排序
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        //显示 2个 cell 的时候,我们才做判断
        if cells.count == 2 {
            
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            //定义一个 indexPath
            var indexPath:NSIndexPath
            
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                
                // 给 indexPath 赋值
                indexPath = self.emoticonPageView.indexPathForCell(firstCell)!
                self.emotionToolBar.setupCurrentButton(indexPath.section)
                
            }else{
                
                //给indexPath 赋值
                indexPath = self.emoticonPageView.indexPathForCell(lastCell)!
                self.emotionToolBar.setupCurrentButton(indexPath.section)
            }
            
            //改变 toolBar 中按钮的状态
            emotionToolBar.setupCurrentButton(indexPath.section)
            
            //设置 pageControl
            setupPageControlData(indexPath)
            
        }
    }
    
    
    //设置 pageControl
    func setupPageControlData(indexPath:NSIndexPath){
        
        pageControl.numberOfPages = HYFEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
    }

}





